package com.fiveGroup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fiveGroup.annotation.Log;
import com.fiveGroup.pojo.SystemNotice;
import com.fiveGroup.pojo.vo.DataVo;
import com.fiveGroup.service.SystemNoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: SystemNoticeController
 * Description: 系统公告控制层
 * 提供发布、查询、修改、删除系统公告的接口
 *
 * @Author wen
 * @Create 2025/9/28 13:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
@Slf4j
public class BackgroundSystemNoticeController {

    private final SystemNoticeService systemNoticeService;


    /**
     * 发布系统公告
     *
     * @return DataVo
     * @RequestBody SystemNotice noticeRequest 公告请求对象，包含标题和内容
     */

    @Log(action = "系统公告", detail = "发布系统公告")
    @PostMapping("/addAnnouncement")
    public DataVo addAnnouncement(@RequestBody SystemNotice noticeRequest) {

        // 创建公告对象并设置属性
        SystemNotice notice = new SystemNotice();
        notice.setTitle(noticeRequest.getTitle());
        notice.setContent(noticeRequest.getContent());
        notice.setCreatedAt(java.time.LocalDateTime.now());
        // 默认启用状态
        notice.setStatus(1);

        // 调用服务层方法保存公告
        systemNoticeService.save(notice);
        return DataVo.success();
    }


    /**
     * 查询系统公告
     *
     * @param currentPage 当前页码
     * @param pageSize    每页数量
     * @param title       标题查询（可选）
     * @return DataVo 包含公告分页列表
     */

    @Log(action = "系统公告", detail = "查询系统公告")
    @PostMapping("/getAnnouncement")
    public DataVo getAnnouncement(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize
            , @RequestParam(required = false) String title
    ) {

        //分页查询公告列表
        IPage<SystemNotice> page = new Page<>(currentPage, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<SystemNotice> queryWrapper = new LambdaQueryWrapper<>();
        if (title != null && !title.isEmpty()) {
            queryWrapper.like(SystemNotice::getTitle, title);
        }
        page.setCurrent(currentPage);
        page.setSize(pageSize);
        systemNoticeService.page(page, queryWrapper);
        return DataVo.success(page);
    }

    /**
     * 修改系统公告
     *
     * @return DataVo
     * @RequestBody SystemNotice notice 包含公告ID和更新后的标题、内容
     */

    @Log(action = "系统公告", detail = "修改系统公告")
    @PostMapping("/updateAnnouncement")
    public DataVo updateAnnouncement(@RequestBody SystemNotice notice) {
        log.info("更新公告: {}", notice);
        // 根据ID更新公告
        systemNoticeService.updateById(notice);
        return DataVo.success();
    }

    /**
     * 修改系统公告状态
     *
     * @param id     公告ID
     * @param status 新状态（1启用，0禁用）
     * @return DataVo
     */

    @Log(action = "系统公告", detail = "修改公告状态")
    @PostMapping("/updateAnnouncementStatus")
    public DataVo updateAnnouncementStatus(@RequestParam Long id, @RequestParam Integer status) {
        SystemNotice notice = systemNoticeService.getById(id);
        //检查公告是否存在
        if (notice == null) {
            return DataVo.fail("公告不存在");
        }
        notice.setStatus(status);
        systemNoticeService.updateById(notice);
        return DataVo.success();
    }

    /**
     * 删除系统公告
     *
     * @param id 公告ID
     * @return DataVo
     */

    @Log(action = "系统公告", detail = "删除系统公告")
    @PostMapping("/deleteAnnouncement")
    public DataVo deleteAnnouncement(@RequestParam Long id) {
        //将状态设置为禁用
        SystemNotice notice = systemNoticeService.getById(id);
        if (notice == null) {
            return DataVo.fail("公告不存在");
        }
        notice.setStatus(0);
        systemNoticeService.updateById(notice);
        // 根据ID删除公告
        systemNoticeService.removeById(id);
        return DataVo.success();
    }
}